package org.benjamin.lending.trusteeship.sumapay;

import org.benjamin.lending.domain.entity.BorrowInfo;
import org.benjamin.lending.domain.entity.MemberInfo;
import org.benjamin.lending.domain.entity.TrusteeshipRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * <h3>项目报备</h3>
 * <p>配合资金托管或存管需要将借款标项目发送到银行报备</p>
 * <p></p>
 *
 * @author 黄文杰
 */
@Component("sumapayBorrowReportOperation")
public class BorrowReportOperation extends SumapayAbstractOperation {
	
	
    @Override
	public void beforeRequest(Map<String, Object> requestMap, String requestId,BorrowInfo borrowInfo) {
		Assert.isNull(borrowInfo.getMemberInfo().getMemberIdentity(),"当前借款人没有录入任何信息");
		super.beforeRequest(requestMap, requestId,borrowInfo);
	}

	@Override
    protected void processRequest(Map<String, Object> requestMap, MemberInfo memberInfo, BorrowInfo borrowInfo, Map<String, Object> parameterMap) {
        requestMap.put("projectCode", borrowInfo.getNo());
        requestMap.put("projectName", borrowInfo.getName());
        requestMap.put("projectType", 0);
        requestMap.put("projectSum", borrowInfo.getTotalMoney());
        requestMap.put("annalRate", borrowInfo.getInvestRate());
        requestMap.put("borrowType", 0);
        requestMap.put("borrowUserIdentity", this.getUserIdIdentity(borrowInfo.getMemberInfo().getUsername()));
        requestMap.put("borrowIdType", 1);
        requestMap.put("borrowIdNumber", borrowInfo.getMemberInfo().getMemberIdentity().getIdentityNumber());
        requestMap.put("borrowerName", borrowInfo.getMemberInfo().getMemberIdentity().getRealName());
    }

    @Override
    protected void buildUserIdIdentity(Map<String, Object> requestMap, String username) {
        //super.buildUserIdIdentity(requestMap, username);
    }

    @Override
    protected void buildAbutmentUrl(Map<String, Object> requestMap) {
        requestMap.put("noticeUrl", sumaplayConfig.getHostName() + "/trus/notice");
        //super.buildAbutmentUrl(requestMap);
    }

    @Override
    protected String signatureRequest(Map<String, Object> requestMap) {
        String[] keys = new String[]{"requestId", "merchantCode","projectCode",
        "projectType","projectSum","annualRate","borrowerType","borrowerUserIdentity",
        "borrowerIdType","borrowerIdNumber","borrowerName","borrowerEnterpriseName",
        "borrowerBizLicenseNo","noticeUrl"};
        String[] temp = this.buildSignature(requestMap, keys);
        requestMap.put("signature",temp[1]);
        return temp[0];
    }

    @Override
    protected void afterRequest(Map<String, Object> parameterMap, MemberInfo memberInfo, BorrowInfo borrowInfo, TrusteeshipRequest trusteeshipRequest) {

    }

    @Override
    protected String[] signatureResponse(Map<String, Object> responseMap) {
        String[] keys = new String[]{"requestId","result","projectCode"};
        return this.buildSignature(responseMap,keys);
    }
}
